package com.starblink.android.guang.btmnav

import android.annotation.SuppressLint
import android.content.Context
import android.graphics.*
import android.graphics.drawable.GradientDrawable
import android.os.Build
import android.util.AttributeSet
import android.widget.LinearLayout
import androidx.annotation.*
import com.google.android.material.bottomnavigation.BottomNavigationView
import com.starblink.android.guang.R
import com.starblink.basic.autosize.utils.AutoSizeUtils
import com.starblink.basic.autosize.utils.ScreenUtils
import kotlin.math.pow
import kotlin.math.sin
import kotlin.math.sqrt


class GuangBottomNavigationView : BottomNavigationView {


    private var pi = 180F

//    private var circleR = AutoSizeUtils.dp2px(context, 42F).toFloat()
//    private var radW =  AutoSizeUtils.dp2px(context, 28F).toFloat()

    private var circleR = AutoSizeUtils.dp2px(context, 36F).toFloat()

    //圆心角 转弧度
    private var rad = 45F
    private var radW = 2 * circleR * sin(Math.toRadians((rad / 2).toDouble())).toFloat()


    private var circleR2 = AutoSizeUtils.dp2px(context, 36F).toFloat()
    private var rad2 = 45F
    private var radW2 = 2 * circleR2 * sin(Math.toRadians((rad2 / 2).toDouble())).toFloat()


    //半径为底的高度 以上边高度分割为两个三角形 小三角形的直角长边
    private var radWTriangleHLong: Float = getRadWTriangleHLong(circleR, radW)

    //以上边高度分割为两个三角形 小三角形的直角短边
    private var radWTriangleHShort: Float = sqrt(radW.pow(2F) - radWTriangleHLong.pow(2F))

    private var radWTriangleHLong2: Float = getRadWTriangleHLong(circleR2, radW2)
    private var radWTriangleHShort2: Float = sqrt(radW2.pow(2F) - radWTriangleHLong2.pow(2F))

    //圆弧的高度
    open var containerArcH = radWTriangleHShort + radWTriangleHShort2


    private var BTMSH = AutoSizeUtils.dp2px(context, 54F).toFloat()

    private val sH: Int = (BTMSH + containerArcH + 0.5F).toInt()

    ///////////////




    private var shadowLength = 6  //阴影大小

    constructor(context: Context) : super(context)

    constructor(context: Context, attrs: AttributeSet) : this(context, attrs, 0)

    constructor(context: Context, attrs: AttributeSet, defStyleAttr: Int) : super(
        context,
        attrs,
        defStyleAttr
    ) {

        background = GradientDrawable().apply { setColor(Color.TRANSPARENT) }
        val ta = context.obtainStyledAttributes(attrs, R.styleable.GuangBottomNavigationView)
        ta.recycle()

    }

    override fun onMeasure(widthMeasureSpec: Int, heightMeasureSpec: Int) {

        super.onMeasure(widthMeasureSpec, heightMeasureSpec)
        //设置自定义底部导航的高
        setMeasuredDimension(
            getDefaultSize(suggestedMinimumWidth, widthMeasureSpec),
            sH + shadowLength
        )


        //设置padding
        setPadding(0,containerArcH.toInt(),0,0)


        invalidate()

    }

    @SuppressLint("DrawAllocation")

    @RequiresApi(api = Build.VERSION_CODES.KITKAT)

    override fun onDraw(canvas: Canvas) {
        super.onDraw(canvas)
        val paint = Paint()
        var pathInner = getArdPath(context, 0, shadowLength.toFloat())
        //关闭硬件加速才能有阴影效果
        setLayerType(LAYER_TYPE_SOFTWARE, paint)
        //按背景色填充背景

        paint.apply {

            style = Paint.Style.FILL
//            if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) {
//                color = backgroundTintList?.defaultColor ?: Color.BLACK
//            }
            color = Color.WHITE
            maskFilter = null
            isAntiAlias = true
            //添加阴影
            setShadowLayer(shadowLength.toFloat(), 0.toFloat(), 0.toFloat(), Color.LTGRAY)

        }
        canvas.drawPath(pathInner, paint)
    }


    //求（circleR 圆半径*2），（radW夹角弦长）的三角形，半径为底的高度
    private fun getRadWTriangleHLong(circleR: Float, radW: Float): Float {
        var halfCircumference = (circleR + circleR + radW) / 2
        var pabc = halfCircumference *
                (halfCircumference - circleR) *
                (halfCircumference - circleR) *
                (halfCircumference - radW)
        return 2 * sqrt(pabc) / circleR
    }


    private fun getArdPath(ctx: Context, delta: Int, incremental: Float?): Path {
        var inc = incremental
        if (inc == null) {
            inc = 0F
        }
        return getCorePath(ctx, inc)
    }

    private fun getCorePath(ctx: Context, incremental: Float): Path {
        var sW = ScreenUtils.getScreenSize(ctx)[0] + 0F

        var startX = sW / 2 - radWTriangleHLong - radWTriangleHLong2
        var p = Path()
        //左下角
        p.moveTo(0F, sH + incremental)
        //左上角
        p.lineTo(0F, containerArcH + incremental)
        //弧形起笔
        p.lineTo(startX, containerArcH + incremental)

        //左弧

        p.arcTo(
            RectF(
                startX - circleR,
                containerArcH - circleR + incremental - circleR,
                startX + circleR,
                containerArcH - circleR + incremental + circleR
            ), pi / 2, -rad,//
            false
        )
        //中部弧线
        p.arcTo(
            RectF(
                sW / 2 - circleR2,
                circleR2 + incremental - circleR2,
                sW / 2 + circleR2,
                circleR2 + incremental + circleR2
            ), pi * 3 / 2 - rad2, 2 * rad2,
            false
        )
        //右弧
        p.arcTo(
            RectF(
                startX + 2 * radWTriangleHLong + 2 * radWTriangleHLong2 - circleR,
                containerArcH - circleR + incremental - circleR,
                startX + 2 * radWTriangleHLong + 2 * radWTriangleHLong2 + circleR,
                containerArcH - circleR + incremental + circleR
            ), (pi / 2 + rad).toFloat(), -rad,
            false
        )

        p.lineTo(
            startX + 2 * radWTriangleHLong + 2 * radWTriangleHLong2,
            (containerArcH + incremental)
        )
        //右上角
        p.lineTo(sW, (containerArcH + incremental))
        //右下角
        p.lineTo(sW, (sH + incremental))
        //左下角
        p.lineTo(0F, (sH + incremental))
        p.close()

        return p
    }
}